home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / xdr_stdio.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  5KB  |  201 lines

  1. /*
  2.  * $Id: xdr_stdio.c,v 1.2 1993/11/14 17:00:03 jraja Exp $
  3.  *
  4.  * $Log: xdr_stdio.c,v $
  5.  * Revision 1.2  1993/11/14  17:00:03  jraja
  6.  * Fixed includes, added ANSI prototypes.
  7.  * Fixed include. Added _M68000 to the processor type list.
  8.  *
  9.  */
  10. /* @(#)xdr_stdio.c    2.1 88/07/29 4.0 RPCSRC */
  11. /*
  12.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  13.  * unrestricted use provided that this legend is included on all tape
  14.  * media and as a part of the software program in whole or part.  Users
  15.  * may copy or modify Sun RPC without charge, but are not authorized
  16.  * to license or distribute it to anyone else except as part of a product or
  17.  * program developed by the user.
  18.  * 
  19.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  20.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  22.  * 
  23.  * Sun RPC is provided with no support and without any obligation on the
  24.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  25.  * modification or enhancement.
  26.  * 
  27.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  28.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  29.  * OR ANY PART THEREOF.
  30.  * 
  31.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  32.  * or profits or other special, indirect and consequential damages, even if
  33.  * Sun has been advised of the possibility of such damages.
  34.  * 
  35.  * Sun Microsystems, Inc.
  36.  * 2550 Garcia Avenue
  37.  * Mountain View, California  94043
  38.  */
  39. #if !defined(lint) && defined(SCCSIDS)
  40. static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro";
  41. #endif
  42.  
  43. /*
  44.  * xdr_stdio.c, XDR implementation on standard i/o file.
  45.  *
  46.  * Copyright (C) 1984, Sun Microsystems, Inc.
  47.  *
  48.  * This set of routines implements a XDR on a stdio stream.
  49.  * XDR_ENCODE serializes onto the stream, XDR_DECODE de-serializes
  50.  * from the stream.
  51.  */
  52.  
  53. #include <sys/param.h>
  54. #include <rpc/types.h>
  55. #include <stdio.h>
  56. #include <rpc/xdr.h>
  57.  
  58. static bool_t    xdrstdio_getlong(XDR * xdrs, long * lp);
  59. static bool_t    xdrstdio_putlong(XDR * xdrs, long * lp);
  60. static bool_t    xdrstdio_getbytes(XDR * xdrs, caddr_t addr, u_int len);
  61. static bool_t    xdrstdio_putbytes(XDR * xdrs, caddr_t addr, u_int len);
  62. static u_int    xdrstdio_getpos(XDR * xdrs);
  63. static bool_t    xdrstdio_setpos(XDR * xdrs, u_int pos);
  64. static long *    xdrstdio_inline(XDR * xdrs, u_int len);
  65. static void    xdrstdio_destroy(XDR * xdrs);
  66.  
  67. /*
  68.  * Ops vector for stdio type XDR
  69.  */
  70. static struct xdr_ops    xdrstdio_ops = {
  71.     xdrstdio_getlong,    /* deseraialize a long int */
  72.     xdrstdio_putlong,    /* seraialize a long int */
  73.     xdrstdio_getbytes,    /* deserialize counted bytes */
  74.     xdrstdio_putbytes,    /* serialize counted bytes */
  75.     xdrstdio_getpos,    /* get offset in the stream */
  76.     xdrstdio_setpos,    /* set offset in the stream */
  77.     xdrstdio_inline,    /* prime stream for inline macros */
  78.     xdrstdio_destroy    /* destroy stream */
  79. };
  80.  
  81. /*
  82.  * Initialize a stdio xdr stream.
  83.  * Sets the xdr stream handle xdrs for use on the stream file.
  84.  * Operation flag is set to op.
  85.  */
  86. void
  87. xdrstdio_create(xdrs, file, op)
  88.     register XDR *xdrs;
  89.     FILE *file;
  90.     enum xdr_op op;
  91. {
  92.  
  93.     xdrs->x_op = op;
  94.     xdrs->x_ops = &xdrstdio_ops;
  95.     xdrs->x_private = (caddr_t)file;
  96.     xdrs->x_handy = 0;
  97.     xdrs->x_base = 0;
  98. }
  99.  
  100. /*
  101.  * Destroy a stdio xdr stream.
  102.  * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
  103.  */
  104. static void
  105. xdrstdio_destroy(xdrs)
  106.     register XDR *xdrs;
  107. {
  108.     (void)fflush((FILE *)xdrs->x_private);
  109.     /* xx should we close the file ?? */
  110. }
  111.  
  112. static bool_t
  113. xdrstdio_getlong(xdrs, lp)
  114.     XDR *xdrs;
  115.     register long *lp;
  116. {
  117.  
  118.     if (fread((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1)
  119.         return (FALSE);
  120. #if !(defined(mc68000) || defined(_M68000))
  121.     *lp = ntohl(*lp);
  122. #endif
  123.     return (TRUE);
  124. }
  125.  
  126. static bool_t
  127. xdrstdio_putlong(xdrs, lp)
  128.     XDR *xdrs;
  129.     long *lp;
  130. {
  131.  
  132. #if !(defined(mc68000) || defined(_M68000))
  133.     long mycopy = htonl(*lp);
  134.     lp = &mycopy;
  135. #endif
  136.     if (fwrite((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1)
  137.         return (FALSE);
  138.     return (TRUE);
  139. }
  140.  
  141. static bool_t
  142. xdrstdio_getbytes(xdrs, addr, len)
  143.     XDR *xdrs;
  144.     caddr_t addr;
  145.     u_int len;
  146. {
  147.  
  148.     if ((len != 0) && (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
  149.         return (FALSE);
  150.     return (TRUE);
  151. }
  152.  
  153. static bool_t
  154. xdrstdio_putbytes(xdrs, addr, len)
  155.     XDR *xdrs;
  156.     caddr_t addr;
  157.     u_int len;
  158. {
  159.  
  160.     if ((len != 0) && (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1))
  161.         return (FALSE);
  162.     return (TRUE);
  163. }
  164.  
  165. static u_int
  166. xdrstdio_getpos(xdrs)
  167.     XDR *xdrs;
  168. {
  169.  
  170.     return ((u_int) ftell((FILE *)xdrs->x_private));
  171. }
  172.  
  173. static bool_t
  174. xdrstdio_setpos(xdrs, pos) 
  175.     XDR *xdrs;
  176.     u_int pos;
  177.  
  178.     return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ?
  179.         FALSE : TRUE);
  180. }
  181.  
  182. static long *
  183. xdrstdio_inline(xdrs, len)
  184.     XDR *xdrs;
  185.     u_int len;
  186. {
  187.  
  188.     /*
  189.      * Must do some work to implement this: must insure
  190.      * enough data in the underlying stdio buffer,
  191.      * that the buffer is aligned so that we can indirect through a
  192.      * long *, and stuff this pointer in xdrs->x_buf.  Doing
  193.      * a fread or fwrite to a scratch buffer would defeat
  194.      * most of the gains to be had here and require storage
  195.      * management on this buffer, so we don't do this.
  196.      */
  197.     return (NULL);
  198. }
  199.  
  200.